<?php

namespace app\wechat\models;

use app\wechat\core\BaseModel;
use cm;
use Yii;
use yii\helpers\Url;

class HotModel extends BaseModel
{
	/*
	*获取热门站点
	*/
	public function actionSite(){
		$ssql = "SELECT id,name,firstchar,'site' as type FROM site WHERE status=1 AND is_hot=1 ORDER BY sort";
		$hotsite = Cm::$db->all($ssql);
		$csql = "SELECT id,name,firstchar,'city' as type FROM open_city WHERE status=1 AND is_hot=1 ORDER BY code";
		$hotcity = Cm::$db->all($csql);
		$hot = [];
		if(!empty($hotsite)){
			foreach ($hotsite as $key => $value) {
				$hot[] = $value; 
			}
		}
		if(!empty($hotcity)){
			foreach ($hotcity as $key => $value) {
				$hot[] = $value; 
			}
		}
		Cm::$res->susess(['hot'=>$hot]);
	}

	/*
	*获取热门线路排版
	*/
	public function  actionCircuit(){
		$page = Cm::$req->post('page', ['null', 'empty', 'default' => 1]);
		$limit = Cm::$req->post('limit', ['null', 'empty', 'default' => 10]);
		$time = date("Y-m-d H:i:s");
		$sql = "SELECT s.id,coa.name AS citysname,cob.name AS cityename,sa.name AS sitesname,sb.name AS siteename,s.s_ticket,s.departuretime,s.ticket_price,s.circuit_id FROM circuit AS a 
				LEFT JOIN open_city coa ON coa.code=a.city_scode 
				LEFT JOIN open_city cob ON cob.code=a.city_ecode
				LEFT JOIN site sa ON sa.id=a.site_sid 
				LEFT JOIN site sb ON sb.id=a.site_eid 
				LEFT JOIN shift s ON s.circuit_id=a.id
				WHERE a.status=1 AND a.is_hot=1 AND s.status=1 AND coa.status=1 AND cob.status=1 AND sa.status=1 AND sb.status=1 AND s.closingtime>='{$time}' AND s.presaletime<='{$time}'
				ORDER BY s.departuretime";
		$resdata = Cm::$db->paging($sql,$page, $limit);	
		if(!empty($resdata['data'])){
			$config = Cm::$db->all("SELECT `key`,`value` FROM config_setting WHERE `group`='sys' AND `key` IN ('sys_service_charge','sys_premium') "); 
			$service_charge = 0;
			$premium = 0;
			if(!empty($config)){
				foreach ($config as $key => $value) {
					if($value['key'] == 'sys_service_charge'){
						$service_charge = floatval($value['value']);	
					}else if($value['key'] == 'sys_premium'){
						$premium = floatval($value['value']);	
					}
				}
			}
			foreach ($resdata['data'] as $key => &$value) {
				$departuretime = strtotime($value['departuretime']);
				$value['day'] = date("m月d日",$departuretime);
				$value['hour'] = date("H:i",$departuretime);
				$value['ticket_price'] += $premium+$service_charge;
				$value['s_ticket'] = $this->checkSticket($value['circuit_id'],$value['id'],$value['s_ticket']);
			}
		}
		Cm::$res->susess($resdata);
	}

	/*
	*获取轮播图
	*/
	public function actionBanner(){
		$list = Cm::$db->all("SELECT `path`,link_url FROM banner WHERE status=1 ORDER BY sort ASC");
		if(!empty($list)){
			foreach ($list as $key => &$value) {
				$value['path'] = BASE_URL.$value['path'];
				$value['link_url'] = empty($value['link_url']) ? '' : $value['link_url'];
			}
		}
		Cm::$res->susess(['banner'=>$list]);
	}
}